<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="value"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button :disabled="!value" @click="onPost" class="post-btn"
      >发布</van-button
    >
  </div>
</template>

<script>
import { PostCommentList } from '@/api/article.js'
import { Toast } from 'vant'
export default {
  name: 'CommentPost',
  components: {},
  props: {
    // 父组件传的值
    target: {
      type: [String, Number, Object],
      require: true
    },
    artId: {
      type: [String, Number, Object],
      // 当对文章进行评论的时候 该值是不需要传的 默认为null
      // 对评论进行评论的时候才需要  再由使用该组件的父组件传过来
      default: null
    }
  },
  data () {
    return {
      message: '',
      value: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 发布评论
    async    onPost () {
      try {
        const res = await PostCommentList({
          target: this.target,
          content: this.value,
          art_id: this.artId
        })
        console.log(res)

        Toast.success('发布成功')
        this.$emit('on-success', res.new_obj)
        this.value = ''
      } catch (err) {
        Toast.fail('发布失败')
      }
    }
  }
}

</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: .4rem 0 .4rem .4rem;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 1.875rem;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
